<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>图书列表展示</title>
  <link rel="stylesheet" href="css/bootstrap.min.css">

  <link rel="stylesheet" href="css/list.css">
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <script type="text/javascript" src="js/bootstrap.min.js"></script>
  <script src="js/jq-paginator.js"></script>

</head>

<body>
<div class="bookContainer">
  <h2>图书列表展示</h2>
  <div class="navbar-justify-between">
    <div>
      <button class="btn btn-outline-info" type="button" onclick="location.href='book_add.html'">添加图书</button>
      <button class="btn btn-outline-info" type="button" onclick="batchDelete()">批量删除</button>
    </div>
  </div>

  <table>
    <thead>
    <tr>
      <td>选择</td>
      <td class="width100">图书ID</td>
      <td>书名</td>
      <td>作者</td>
      <td>数量</td>
      <td>定价</td>
      <td>出版社</td>
      <td>状态</td>
      <td class="width200">操作</td>
    </tr>
    </thead>
    <tbody>

    </tbody>
  </table>
  <div class="demo">
    <ul id="pageContainer" class="pagination justify-content-center"></ul>
  </div>
  <script>
    getBookList();
    function getBookList() {
      $.ajax({
        type:"get",
        url:"/book/getlistpage"+location.search,
        success:function(result){
          if(result==null||result.data==null){
            location.href="book_list.html";
            return;
          }
          //打印数据
          var tbody = $("tbody");
          tbody.empty(); //清空表格内容
          result.data.records.forEach(function(book) {
            var statusText = book.status === 1 ? "可借阅" : "不可借阅";
            var row = `<tr>
                                <td><input type="checkbox" name="selectBook" value="${book.id}" class="book-select"></td>
                                <td>${book.id}</td>
                                <td>${book.bookName}</td>
                                <td>${book.author}</td>
                                <td>${book.count}</td>
                                <td>${book.price.toFixed(2)}</td>
                                <td>${book.publish}</td>
                                <td>${statusText}</td>
                                <td>
                                    <div class="op">
                                        <a href="book_update.html?bookId=${book.id}">修改</a>
                                        <a href="javascript:void(0)" onclick="deleteBook(${book.id})">删除</a>
                                    </div>
                                </td>
                            </tr>`;
            tbody.append(row);
          });
          console.log(result.data);
          //处理翻页信息
          var pagerequest=result.data.pageRequest;
          $("#pageContainer").jqPaginator({
            totalCounts: result.data.total, //总记录数
            pageSize: 10,    //每页的个数
            visiblePages: 5, //可视页数
            currentPage: pagerequest.currentPage,  //当前页码
            first: '<li class="page-item"><a class="page-link">首页</a></li>',
            prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页<\/a><\/li>',
            next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页<\/a><\/li>',
            last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页<\/a><\/li>',
            page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}<\/a><\/li>',
            //页面初始化和页码点击时都会执行
            onPageChange: function (page, type) {
              console.log("第"+page+"页, 类型:"+type);
              if(type === "init") {
                //初始化时不需要跳转
                return;
              }else if(type === "change")
                      //点击页码时跳转
                console.log("跳转到第"+page+"页");
              //""传的是参数
              location.href = "book_list.html?currentPage=" + page;
            }
          });
        }
      })
    }

    function deleteBook(id) {
      var isDelete = confirm("确认删除?");
      if (isDelete) {
        //删除图书
        $.ajax({
          type:"get",
          //bookid后端参数获取前端的id参数
          url:"/book/getdelete?bookid="+id,
          success:function(data){
            if(data){
              alert("删除成功");
            }else{
              alert("删除失败");
            }
            getBookList(); //重新获取图书列表
          }
        });
      }
    }
    function batchDelete() {
      var isDelete = confirm("确认批量删除?");
      if (isDelete) {
        //获取复选框的id
        var ids = [];
        $("input:checkbox[name='selectBook']:checked").each(function () {
          ids.push($(this).val());
        });
        console.log(ids);
        $.ajax({
          type:"put",
          url:"/book/getBacthDelete?ids="+ids,
          success:function(data){
            if(data){
                alert("删除成功");
              }else{
                alert("删除失败");
              }
              getBookList(); //重新获取图书列表
          }
        })
        alert("批量删除成功");
      }
    }
  </script>
</div>
</body>

</html>